<script setup lang="ts">

import {Delete, Edit, Plus, Refresh, Search} from "@element-plus/icons-vue";
import {ref} from "vue";
import request from "@/utils/request";
import {ElMessage} from "element-plus";
//表单验证规则
const elRef = ref(null);
const rules = ref({
    platformKey: [{
        required: true,
        message: '请输入文章的Key',
        trigger: 'blur'
    }],
    title: [{
        required: true,
        message: '请输入标题',
        trigger: 'blur'
    }],
    content: [{
        required: true,
        message: '请输入内容',
        trigger: 'blur'
    }]
});

//加载页面数据======================================================
function getPageData(query) {
    return request({
        url: '/platform-info/list',
        method: 'get',
        params: query
    });
}

const searchData = ref({
    platformKey: '',
    title: '',
    pageSize: 10,
    pageNum: 1
});
const pageData = ref<object[]>([]);
getPageData(searchData.value).then((res) => {
    console.log(res);
    pageData.value?.push((res as any).rows);
    pageDatatotal.value = (res as any).total;
});

//查询==================================================================


function search() {
    pageData.value.length = 0;
    getPageData(searchData.value).then((res) => {
        pageData.value?.push((res as any).rows);
        pageDatatotal.value = (res as any).total;
    });
}

//重置================================================================
function reset() {
    searchData.value.platformKey = '';
    searchData.value.title = '';
    pageData.value.length = 0;
    getPageData(searchData.value).then((res) => {
        pageData.value?.push((res as any).rows);
        pageDatatotal.value = (res as any).total;
    });
}

//新增==================================================================
const addDialog = ref(false);
const addformData = ref({
    platformKey: '',
    title: '',
    content: '',
    remark: ''
});

function addNewData() {
    addDialog.value=true;
}
function addNewDataFn(query){
    return request({
        url: '/platform-info',
        method: 'post',
        data: query
    });
}
function addsubmitForm() {
    (elRef as any).value.validate((valid) => {
        if (valid) {
            addNewDataFn(addformData.value).then(()=>{
                ElMessage.success('提交成功');
                pageData.value.length = 0;
                getPageData(searchData.value).then((res) => {
                    pageData.value?.push((res as any).rows);
                    pageDatatotal.value = (res as any).total;
                });
                addDialog.value=false;
            });

        } else {
            ElMessage.error('提交失败');
            return false;
        }
    });
};

//修改单条数据=============================================
const changeDialog=ref(false);
const changeformData = ref({
    platformKey: '',
    title: '',
    content: '',
    remark: '',
    platformId:'',
});
function changeOnceData(id) {
    changeformData.value.platformId=id;
    changeDialog.value=true;
    getOnceData(id).then(res=>{
        changeformData.value.platformKey=res.data.platformKey;
        changeformData.value.title=res.data.title;
        changeformData.value.content=res.data.content;
        changeformData.value.remark=res.data.remark;
    });
}
function getOnceData(id){
    return request({
        url: `/platform-info/${id}`,
        method: 'get',
    });
}
function changeDataFn(query){
    return request({
        url: '/platform-info',
        method: 'put',
        data: query
    });
}
function changesubmitForm() {
    (elRef as any).value.validate((valid) => {
        if (valid) {
            changeDataFn(changeformData.value).then(()=>{
                ElMessage.success('提交成功');
                pageData.value.length = 0;
                getPageData(searchData.value).then((res) => {
                    pageData.value?.push((res as any).rows);
                    pageDatatotal.value = (res as any).total;
                });
                changeDialog.value=false;
            });

        } else {
            ElMessage.error('提交失败');
            return false;
        }
    });
};
//删除单条数据========================================
const deleteDialog = ref(false);
const DeleteId = ref();

function deleteDataFn(id) {
    return request({
        url: `/platform-info/${id}`,
        method: 'delete',
    });
}

function deleteOnceData(id) {
    DeleteId.value = id;
    deleteDialog.value = true;
}

function sureDelete() {
    deleteDataFn(DeleteId.value).then(() => {
        ElMessage.success('删除成功');
        pageData.value.length = 0;
        getPageData(searchData.value).then((res) => {
            pageData.value?.push((res as any).rows);
            pageDatatotal.value = (res as any).total;
        });
        deleteDialog.value = false;
    });
}

//分页===================================================
const pageDatatotal = ref(0);
const handleCurrent = (val: number) => {
    console.log(`current page: ${val}`);
    searchData.value.pageNum = val;
    pageData.value.length = 0;
    getPageData(searchData.value).then(res => {
        pageData.value?.push((res as any).rows);
    });
};

//取消操作===================================
function cencelAction() {
    deleteDialog.value = false;
    addDialog.value = false;
    changeDialog.value=false;
    ElMessage('取消操作');
}
</script>
<template>
    <div>
        <div class="title" style="position: relative">
            <span class="inputTitle">文章的key</span>
            <el-input
                v-model="searchData.platformKey"
                class="activeName"
                placeholder="请输入文章的key"
                clearable
                size="large"
            />
            <span class="inputTitle">标题</span>
            <el-input
                v-model="searchData.title"
                class="activeName"
                placeholder="请输入标题"
                clearable
                size="large"
            />
            <span class="btns">
            <el-button type="primary" :icon="Search" class="serachBtn" @click="search">查询</el-button>
            <el-button :icon="Refresh" class="refreshBtn" @click="reset">重置</el-button>
        </span>
        </div>
        <section class='tableBox' style="padding: 0 30px">
            <el-button type="primary" :icon="Plus" style="margin-top: 20px;" @click="addNewData">新增</el-button>
            <el-table :data="(pageData[0] as any)" style="width: 100%;margin-top: 50px" stripe>
                <template #empty>
                    <el-empty description="暂无数据"/>
                </template>
                <el-table-column prop="platformKey" label="文章的Key" height/>
                <el-table-column prop="title" label="标题" height/>
                <el-table-column prop="操作" label="操作" height>
                    <template #default="index">
                        <el-button style="color: #1c84c6;" @click="changeOnceData(index.row.platformId)" :icon="Edit">修改</el-button>
                        <el-button style="color: #1c84c6;margin-left: 20px" @click="deleteOnceData(index.row.platformId)" :icon="Delete">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </section>
        <div style="position: relative">
            <span style="margin-left: 1000px;position: absolute;top: 8px">共{{ pageDatatotal }}条数据</span>
            <el-pagination
                style="margin-top: 20px;margin-left: 1150px"
                :page-size="searchData.pageSize"
                layout="prev, pager, next"
                :total="pageDatatotal"
                @currentChange="handleCurrent"
            />
        </div>
        <el-dialog v-model="deleteDialog" title="提示" width="500" center>
            <el-icon color="rgb(255, 186, 0)" size="25">
                <Warning/>
            </el-icon>
            <span style="font-size: 20px;vertical-align: top;margin-left: 10px">进行删除操作后无法恢复，是否继续删除？</span>

            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="cencelAction">取消</el-button>
                    <el-button type="primary" @click="sureDelete">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>
        <el-dialog v-model="addDialog" title="新增" width="800" style="padding: 20px">
            <el-form ref="elRef" :model="addformData" :rules="rules">
                <el-form-item label="文章的key：" prop="platformKey">
                    <el-input
                        v-model="addformData.platformKey"
                        placeholder="请输入文章的key"
                        clearable
                        size="large"></el-input>
                </el-form-item>
                <el-form-item label="标题：" prop="title">
                    <el-input
                        v-model="addformData.title"
                        placeholder="请输入标题"
                        clearable
                        size="large"></el-input>
                </el-form-item>
                <el-form-item label="内容：" prop="content">
                    <editor
                        v-model="addformData.content"
                        placeholder="请输入文章内容"
                        clearable
                        size="large"></editor>
                </el-form-item>
                <el-form-item label="备注：" prop="remark">
                    <el-input
                        v-model="addformData.remark"
                        type="textarea"
                        resize="none"
                        placeholder="请输入文章内容"
                        clearable
                        size="large"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer" style="margin-left: 560px">
                    <el-form-item>
                        <el-button type="primary" size="large" @click="addsubmitForm">确定</el-button>
                        <el-button size="large" @click="cencelAction">取消</el-button>
                    </el-form-item>
                </div>
            </template>
        </el-dialog>
        <el-dialog v-model="changeDialog" title="修改" width="800" style="padding: 20px">
            <el-form ref="elRef" :model="changeformData" :rules="rules">
                <el-form-item label="文章的key：" prop="platformKey">
                    <el-input
                        v-model="changeformData.platformKey"
                        placeholder="请输入文章的key"
                        clearable
                        size="large"></el-input>
                </el-form-item>
                <el-form-item label="标题：" prop="title">
                    <el-input
                        v-model="changeformData.title"
                        placeholder="请输入标题"
                        clearable
                        size="large"></el-input>
                </el-form-item>
                <el-form-item label="内容：" prop="content">
                    <editor
                        v-model="changeformData.content"
                        placeholder="请输入文章内容"
                        clearable
                        size="large"></editor>
                </el-form-item>
                <el-form-item label="备注：" prop="remark">
                    <el-input
                        v-model="changeformData.remark"
                        type="textarea"
                        resize="none"
                        placeholder="请输入文章内容"
                        clearable
                        size="large"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer" style="margin-left: 560px">
                    <el-form-item>
                        <el-button type="primary" size="large" @click="changesubmitForm">确定</el-button>
                        <el-button size="large" @click="cencelAction">取消</el-button>
                    </el-form-item>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<style scoped>
.title {
    height: 65px;
    border-bottom: 1px solid rgb(237, 237, 237);
    line-height: 65px;
    padding: 0 45px;
    overflow: hidden;
}

.inputTitle {
    font-size: 14px;
    font-weight: bolder;
    color: #666;

}

.activeName {
    width: 15%;
    margin-left: 15px;
    margin-right: 15px;
    height: 28px;
    font-size: 12px;
}

/deep/ .cell {
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 16px !important;
}
/deep/.el-form-item__label{
    width: 15% !important;
}
/deep/ .ql-editor{
    min-height: 300px;
}
</style>
